package com.twu.biblioteca.instruction;

import com.twu.biblioteca.behavior.Behavior;
import com.twu.biblioteca.behavior.ReserveBehavior;

import java.util.regex.Pattern;

public class ReserveInstruction implements Instruction {
    private static final String COMMAND_PATTERN = "^r\\s+\\d+$";
    private String input;
    private int bookIndex;

    @Override
    public Behavior getBehavior() {
        return new ReserveBehavior(getBookIndex());
    }

    @Override
    public boolean isThisInstruction(String s) {
        return Pattern.compile(COMMAND_PATTERN).matcher(s.toLowerCase().trim()).matches();
    }

    @Override
    public String getDescription() {
        return "r: reserve a book. " +
                "If there is a book whose index is 23 in library, " +
                "then you can use this command like this: \"r 23\"";
    }

    @Override
    public void setInput(String input) {
        this.input = input.trim();
    }

    int getBookIndex() {
        return Integer.valueOf(input.replaceAll("^(r|R)\\s+", ""));
    }
}
